package com.zxl.diy.weather.model;

import android.content.Context;
import android.os.Build;
import android.provider.Settings;

import com.zxl.diy.weather.R;
import com.zxl.manager.privacy.utils.track.crash.CrashReportManager;
import com.zxl.manager.privacy.utils.utils.MachineUtils;

import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Administrator on 2016/11/5.
 */

public class PheadHelper {

    public static final String PROTOCOL_VERSION = "2.0";

    public static final String BASE_SERVICE_URL = "http://goweatherex.3g.cn/goweatherex/";

    public static Map<String, String> creatPheadHelp(Context context) {
        try {
            Map<String, String> header = new HashMap<>();
            header.put("lang", getLanguage(context));
            header.put("sys", android.os.Build.VERSION.RELEASE);
            header.put("ps", PheadHelper.PROTOCOL_VERSION);
            header.put("chan", String.valueOf(100)); // 产品id
            header.put("cliId", PheadHelper.getAndroidId(context)); // android id
            header.put("gliId", PheadHelper.getGOID(context)); // go id
            header.put("chanId", CrashReportManager.getInstance().channel); // 渠道号
            return header;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取客户端手机的语言,注意是帶地區的
     * @param context
     *
     * @return
     */
    public static String getLanguage(Context context) {

        String language = "zh_CN";
        try {
            Locale locale = context.getResources().getConfiguration().locale;
            language = locale.getLanguage() + "_" + locale.getCountry();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return language;
    }

    public static boolean isSuccessFromService(JSONObject jsonObject) {
        JSONObject head = jsonObject.optJSONObject("head");
        return null != head && head.optInt("result") == 1;
    }

    public static String initDb(Context context) {
        File dbFile = new File(context.getFilesDir() + "/city/zxl_city.db");
        if (!dbFile.exists()) {
            File parentFile = dbFile.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdir();
            }
            InputStream is = null;
            FileOutputStream fos = null;
            try {
                is = context.getResources().openRawResource(R.raw.zxl_city);
                fos = new FileOutputStream(dbFile);
                final int bufferLength = 8192;
                byte[] buffer = new byte[bufferLength];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return dbFile.getPath();
    }

    /**
     * 获取android_id
     *
     * @param context
     * @return
     */
    public static String getAndroidId(Context context) {
        try {
            return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return UUID.randomUUID().toString();
    }

    public static String getGOID(Context context) {
        return getAndroidId(context) + "ssssdffe";
    }
}
